#include <SoftwareSerial.h>

#define LedPin 9
#define IDR A0
#define PIF A1   //F is stand for Left
#define PIS A2   //S is stand for Right
unsigned long lastMs=0;
unsigned long lastMsBri=0;
unsigned long lastMsAut=0;  //Aut means AutoCtronl for Global_System
unsigned long lastMsCheck=0;
unsigned long lastMsTF=0;   //F is stand for Left
unsigned long lastMsTS=0;   //S is stand for Right
SoftwareSerial mySerial(6,7);

void setup() {
  Serial.begin(57600);
  mySerial.begin(57600);
  pinMode(LedPin, OUTPUT);
  digitalWrite(LedPin,LOW);
  pinMode(IDR,INPUT);
  pinMode(PIF,INPUT);
  pinMode(PIS,INPUT);
}

void loop() {
    int static currentTime;
    String static readData = "";
    int static aut_switch=0;
    int static led_switch=0;
    int static idr_switch=0;
    int static pif_switch=0;
    int static pis_switch=0;
    int static atm_switch=0;
    int static brightness;

//  软串口发送LED数据
    
    if(millis()-lastMs>60000){
      mySerial.print("XX"+String(currentTime)+"Y");
      lastMs=millis();
    }

    if(millis()-lastMsBri>3223){
      mySerial.print("GG"+String(brightness)+"H");
      lastMsBri=millis();
    }

    if(millis()-lastMsCheck>47333){
      mySerial.print("WLED"+String(led_switch));
      delay(500);
      mySerial.print("WIDR"+String(idr_switch));
      delay(500);
      mySerial.print("WPIF"+String(pif_switch));
      delay(500);
      mySerial.print("WPIS"+String(pis_switch));
      delay(500);
      mySerial.print("QAUT"+String(aut_switch));
      delay(500);
      mySerial.print("ZATM"+String(atm_switch));  
      Serial.println("WLED"+String(led_switch));
      delay(500);
      Serial.println("WIDR"+String(idr_switch));
      delay(500);
      Serial.println("WPIR"+String(pif_switch));
      delay(500);
      Serial.println("WPIR"+String(pis_switch));
      delay(500);
      Serial.println("QAUT"+String(aut_switch));
      delay(500);
      Serial.println("ZATM"+String(atm_switch));     
      lastMsCheck=millis();
    }

    if(mySerial.available()){
      char c=mySerial.read();
      if(c=='D'){
        readData=readDfunc();
      }
      if(c=='T'){
        currentTime=readTfunc();
      }
      if(c=='G'){
        brightness=readBfunc();
        analogWrite(LedPin,brightness); 
        if(currentTime>18 and currentTime<6){
          idr_switch=0;     
        }
        if(currentTime<19 and currentTime>5){
          idr_switch=1;  
        }
        
        mySerial.print("WIDR0");
        Serial.println("WIDR0");
      }else{
        Serial.println("No strings");
      }
    }

    Serial.print("NOW TIME IS::");
    Serial.println(currentTime);
    
    Serial.print("readData is this ::");
    Serial.println(readData);

    Serial.print("brightness is this ::");
    Serial.println(brightness);

    autfunc(&readData,&aut_switch,&atm_switch);
    atmfunc(&readData,&atm_switch);

    if(aut_switch==1){
      if(millis()-lastMsAut>7777*2){
        if(currentTime>18 and currentTime<24){
          if(led_switch==0||idr_switch==0){
            led_switch=1;idr_switch=1;
            mySerial.print("WLED1");
            delay(1000);
            mySerial.print("WIDR1");
          }
        }
        if(currentTime>=0 and currentTime<7){
          if(led_switch==0||idr_switch==0||pif_switch==0||pis_switch==0){
            led_switch=1;idr_switch=1;pif_switch=1;pis_switch=1;  
            mySerial.print("WLED1");
            delay(1000);
            mySerial.print("WIDR1");
            delay(1000);
            mySerial.print("WPIF1");
            delay(1000);
            mySerial.print("WPIS1");
          }
        }
        if(currentTime>6 and currentTime<19){
          if(led_switch==1||idr_switch==1||pif_switch==1||pis_switch==1){
            digitalWrite(LedPin,LOW);
            brightness=0;
            led_switch=0;idr_switch=0;pif_switch=0;pis_switch=0;
            mySerial.print("WLED0");
            delay(1000);
            mySerial.print("WIDR0");
            delay(1000);
            mySerial.print("WPIF0");
            delay(1000);
            mySerial.print("WPIS0");
          }
        }
        lastMsAut=millis();
      }
    }
    
    dataUtils(&readData,&led_switch,&idr_switch,&pif_switch,&pis_switch);
    
//    if(led_switch==1 and idr_switch==0 and pir_switch==0){
//      switchfunc(&readData,&led_switch);
//    }

    int y;
    if(led_switch==1 and idr_switch==1){
      y=idrfunc(&brightness,currentTime);
    }

    if(led_switch==1 and idr_switch==1 and pif_switch==1){
      piffunc(y,&LastMsTF);
    }
    if(led_switch==1 and idr_switch==1 and pis_switch==1){
      pisfunc(y,&LastMsTS);
    }
    if(LastMsTF>0 and LastMsTS>0){
      LastTUtils(&LastMsTF,&LastMsTS);
    }
    if(LastMsTF==0 || LastMsTS==0){
      mySerial.print("VN");
    }
    if(readData=="PTL1"){
      while(y<100){
          analogWrite(LedPin,idr_val);
          idr_val++;
          delay(10);
        }
        delay(2000);  //持续高亮时长
        while(y>20){
          analogWrite(LedPin,idr_val);
          idr_val--;
          delay(10);
        }
    }
    
//    Serial.print("led_switch:");Serial.println(led_switch);
//    Serial.print("idr_switch:");Serial.println(idr_switch);
//    Serial.print("pir_switch:");Serial.println(pir_switch);
    delay(1000);
}

  /*读取命令数据*/

String readDfunc(){
    char s;
    String readData="";
    s=(char)mySerial.read();
    if(s=='L'||s=='I'||s=='P'||s=='A'){
      readData+=s; 
      while(true){
        s=(char)mySerial.read();
        if(s=='1'){
          readData+='1'; 
          return readData;
        }
        if(s=='0'){
          readData+='0'; 
          return readData;
        }
        else{
          readData+=s; 
        }
      }
    }
    else{
//      Serial.println("not any data");
      readData="";
      return readData;
    }
  }

  /*读取时间数据*/

int readTfunc(){
  char c;
  String readTime="";
  while(true){
    c=(char)mySerial.read();
    if(c=='E'){
      return readTime.toInt();
    }else{
      readTime+=c;
    }
  }
}

  /*读取亮度数据*/
int readBfunc(){
  char c;
  String readBri="";
  while(true){
    c=(char)mySerial.read();
    if(c=='H'){
      return readBri.toInt();
    }else{
      readBri+=c;
    }
  }
}

void LastTUtils(unsigned long* LastMsTF,unsigned long* LastMsTS){
  if(*LastMsTF<*LastMsTS){
    mySerial.print("VS");
    delay(5000); //延时时长为右边下一个路灯变亮再变暗的时长
    *LastMsTF=0;
    *LastMsTS=0;
  }
  if(*LastMsTS<*LastMsTF){
    mySerial.print("VF");
    delay(5000); //延时时长为左边下一个路灯变亮再变暗的时长
    *LastMsTS=0;
    *LastMsTF=0;
  }
}

  /*解析数据*/
void dataUtils(String* readData,int* led_switch,int* idr_switch,int* pir_switch){
  if(*readData=="LED1"){*led_switch=1;digitalWrite(LedPin,HIGH);mySerial.print("WLED1");};
  if(*readData=="LED0"){*led_switch=0;digitalWrite(LedPin,LOW);mySerial.print("WLED0");};
  if(*readData=="IDR1"){*idr_switch=1;mySerial.print("WIDR1");};
  if(*readData=="IDR0"){*idr_switch=0;mySerial.print("WIDR0");};
  if(*readData=="PIR1"){*pir_switch=1;mySerial.print("WPIR1");};
  if(*readData=="PIR0"){*pir_switch=0;mySerial.print("WPIR0");};
  *readData="";
}
  
//  /*开关阶段*/
//  void switchfunc(String* readData,int* led_switch){
//    String static beforeState = "LED0";
//    
//    if( *readData=="LED0" and beforeState=="LED1"){
////      Serial.println("LED:OFF");
//      digitalWrite(LedPin,LOW);  //Turn off
//      delay(10);
//      beforeState="LED0";
//    }
//    if( *readData=="LED1" and beforeState=="LED0"){
////      Serial.println("LED:ON");
//      digitalWrite(LedPin,HIGH);  //Turn on
//      delay(10);
//      beforeState="LED1";
//    }
//  }

  /*光敏阶段*/
  int idrfunc(int* brightness,int t){
    
    int y;
    int catchIdr=analogRead(IDR);
    if(t>18 and t<24){
      int idr_val=map(catchIdr,0,1023,0,255); 
      *brightness=idr_val;
      /* 实现随着光强增加LED变亮
         map函数 将0~1023范围映射到0~255 */
//           Serial.println(idr_val);
      if(idr_val>20 and idr_val<256){
        analogWrite(LedPin,idr_val);
      }else{
        digitalWrite(LedPin,LOW);
      }
      y=0;
      return y;
    }
    if(t>=0 and t<6){
      int idr_val=map(catchIdr,0,1023,255,0);
      idr_val+=10; 
      analogWrite(LedPin,idr_val);
      y=idr_val;
      return y;
    }
    if(t>5 and t<19){
      y=0;
      return y;
    }
  }

 /*红外f阶段*/
  void piffunc(int idr_val,unsigned long* LastMsT){
    if(idr_val>0){
//       Serial.println("PIR:ON");
       int pir_val=digitalRead(PIF);
//        Serial.println(pir_val);
        if(pir_val==1){
          *LastMsT=millis();
        while(idr_val<100){
          analogWrite(LedPin,idr_val);
          idr_val++;
          delay(10);
        }
        delay(2000);  //持续高亮时长
        while(idr_val>20){
          analogWrite(LedPin,idr_val);
          idr_val--;
          delay(10);
        }
      }else{
        delay(100);
      }
    }
}

 /*红外s阶段*/
public void pisfunc(int idr_val,unsigned long* LastMsT){
    if(idr_val>0){
//       Serial.println("PIR:ON");
       int pir_val=digitalRead(PIS);
//        Serial.println(pir_val);
        if(pir_val==1){
          *LastMsT=millis();
        while(idr_val<100){
          analogWrite(LedPin,idr_val);
          idr_val++;
          delay(10);
        }
        delay(2000);  //持续高亮时长
        while(idr_val>20){
          analogWrite(LedPin,idr_val);
          idr_val--;
          delay(10);
        }
      }else{
        delay(100);
      }
    }
}

void atmfunc(String* readData,int* atm_switch){
  if(*readData=="ATM1"){
    if(*atm_switch==0){
      *atm_switch=1;
      *readData="";
      mySerial.print("ZATM1"); 
    }
  }
  if(*readData=="ATM0"){
    if(*atm_switch==1){
      *atm_switch=0;
      *readData="";
      mySerial.print("ZATM0"); 
    }
  }
}

void autfunc(String* readData,int* aut_switch,int* atm_switch){
  if(*readData=="AUT1"){
    if(*aut_switch==0){
      *aut_switch=1;
      *atm_switch=1;
      *readData="";
      mySerial.print("QAUT1");
      delay(1000);
      mySerial.print("ZATM1");  
    }
  }
  if(*readData=="AUT0"){
    if(*aut_switch==1){
      *aut_switch=0;
      *atm_switch=0;
      *readData="";
      mySerial.print("QAUT0");
      delay(1000);
      mySerial.print("ZATM0"); 
    }
  }
}
  
